package com.qmx.qmxapp.activity;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.base.BaseActivity;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.token.TokenManager;
import com.qmx.qmxapp.utils.DateTimeUtils;
import com.qmx.qmxapp.utils.StringUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author ChengXiangtong
 * @create 2018/10/29
 * @Describe 预约会议
 */
public class AppointmentMeetingActivity extends BaseActivity {
    @BindView(R.id.tv_right_label)
    TextView tv_right_label;
    @BindView(R.id.head_back)
    ImageView head_back;
    @BindView(R.id.title_content)
    TextView title_content;
    @BindView(R.id.ed_appointment_meeting_name)
    EditText ed_appointment_meeting_name;
    @BindView(R.id.rv_start_time)
    RelativeLayout rv_start_time;
    @BindView(R.id.rv_end_time)
    RelativeLayout rv_end_time;
    @BindView(R.id.tv_start_time)
    TextView tv_start_time;
    @BindView(R.id.tv_end_time)
    TextView tv_end_time;
    @BindView(R.id.tv_meeting_details)
    TextView tv_meeting_details;
    @BindView(R.id.rv_meeting_join_persons)
    RelativeLayout rv_meeting_join_persons;
    @BindView(R.id.tv_meeting_join_persons)
    TextView tv_meeting_join_persons;

    private TimePickerView pvTime;
    private String start_date;
    private String end_date;
    private List<String> uids = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appointment_meeting);
        ButterKnife.bind(this);
        title_content.setText("预约会议");
        tv_right_label.setText("提交");

    }

    @OnClick({R.id.head_back, R.id.tv_right_label, R.id.rv_start_time, R.id.rv_end_time, R.id.tv_meeting_details, R.id.rv_meeting_join_persons})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.head_back:
                onBackPressed();
                break;
            case R.id.tv_right_label:
                submitAppointmentMeeting();
                //                Log.i("uids.toString()", );
                break;
            case R.id.rv_start_time:
                meetingDate(true);
                pvTime.show();
                break;
            case R.id.rv_end_time:
                meetingDate(false);
                pvTime.show();
                break;
            case R.id.tv_meeting_details:
                startActivityForResult(new Intent(this, MeetingDetailsAddActivity.class), 1);
                break;
            case R.id.rv_meeting_join_persons:
                startActivityForResult(new Intent(this, ConfereeActivity.class), 2);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == 1)
            tv_meeting_details.setText("会议详情：" + data.getStringExtra("content"));
        if (resultCode == 2) {
            if (!data.getStringArrayListExtra("uids").isEmpty()) {
                uids = data.getStringArrayListExtra("uids");
                tv_meeting_join_persons.setText(uids.size() + "");
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public void submitAppointmentMeeting() {
        String main_id = String.valueOf(sharedPreferences.getInt(AppConstant.ROOM_ID, 123));


        OkGo.<LzyResponse<String>>post(AppConstant.Url + ComParamContact.ReserveMeeting.PATH)
                .tag(this)
                .headers(ComParamContact.ReserveMeeting.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .params(ComParamContact.ReserveMeeting.TITLE, ed_appointment_meeting_name.getText().toString())
                .params(ComParamContact.ReserveMeeting.DETAIL, tv_meeting_details.getText().toString())
                .params(ComParamContact.ReserveMeeting.UIDS, StringUtils.list2Str(uids))
                .params(ComParamContact.ReserveMeeting.MAIN_ID, main_id)
                .params(ComParamContact.ReserveMeeting.START_TIME, start_date)
                .params(ComParamContact.ReserveMeeting.END_TIME, end_date)
                .params(ComParamContact.ReserveMeeting.NOTICE, 0)
                .params(ComParamContact.ReserveMeeting.__TOKEN__, sharedPreferences.getString(AppConstant.CLIENT_ID, "0"))
                .execute(new JsonCallback<LzyResponse<String>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<String>> response) {
                        showToast("成功");
                        Log.i("onSuccess", response.toString());
                    }

                    @Override
                    public void onError(Response<LzyResponse<String>> response) {
                        showToast("失败");
                        Log.i("失败", response.toString());
                    }
                });
    }

    public void meetingDate(final boolean StartOrEnd) {
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.setTime(new Date());//设置当前日期
        int year = selectedDate.get(Calendar.YEAR);//获取年份
        int month = selectedDate.get(Calendar.MONTH);//获取月份
        int day = selectedDate.get(Calendar.DATE);//获取日
        Calendar startDate = Calendar.getInstance();
        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);

        //正确设置方式 原因：注意事项有说明
        startDate.set(year, month, day);
        endDate.set(2020, 11, 31);

        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                if (StartOrEnd) {
                    start_date = String.valueOf(date.getTime());
                    tv_start_time.setText(StringUtils.formatDate(date, DateTimeUtils.FORMAT_LONG));
                } else {
                    end_date = String.valueOf(date.getTime());
                    tv_end_time.setText(StringUtils.formatDate(date, DateTimeUtils.FORMAT_LONG));
                }
            }
        })
                .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setContentTextSize(14)//滚轮文字大小
                .setTitleSize(16)//标题文字大小
                .setTitleText("")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.WHITE)//确定按钮文字颜色
                .setCancelColor(Color.WHITE)//取消按钮文字颜色
                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(true)//是否显示为对话框样式
                .build();
        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
    }
}
